package com.xjysx.xiaochetong.moudle;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.WindowManager;
import android.widget.ImageView;

import com.xjysx.xiaochetong.R;
import com.xjysx.xiaochetong.base.BaseActivity;
import com.xjysx.xiaochetong.network.BaseBean;
import com.xjysx.xiaochetong.network.BaseObserver;
import com.xjysx.xiaochetong.network.Network;
import com.xjysx.xiaochetong.util.PollingUtil;
import com.xjysx.xiaochetong.util.QRCodeUtil;
import com.xjysx.xiaochetong.util.SignatureUtil;
import com.xjysx.xiaochetong.util.ToastUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import static com.xjysx.xiaochetong.network.Network.OPEN;

/**
 * Created by danjj on 2020/8/16 0016.
 */
public class QrCodeActivity extends BaseActivity {
    @BindView(R.id.iv_qrcode)
    ImageView ivQrcode;

    String qrcode_sn = "";

    int num = 0;
    boolean isSuccess = false;

    private BaseObserver<Object> observer = new BaseObserver<Object>() {

        @Override
        public void onSuccess(Object bean) {
            isSuccess = true;
            Intent intent = new Intent(getContext(),SelectActivity.class);
            startActivity(intent);
            finish();
        }

        @Override
        public void onError(int code, String message, BaseBean baseBean) {
//            Intent intent = new Intent(getContext(),SelectActivity.class);
//            startActivity(intent);
//            finish();
            dismissDialog();
//            ToastUtil.showToast(getContext(), message);
        }

        @Override
        public void networkError(Throwable e) {
            dismissDialog();
//            ToastUtil.showToast(getContext(), getContext().getResources().getString(R.string.network_error));
        }
    };
    private PollingUtil pollingUtil;
    private Runnable runnable;
    private Runnable runnable_heart;

    @Override
    protected void loadLayout() {
        setContentView(R.layout.activity_qrcode);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    }

    @Override
    public void initPresenter() {
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {
        super.processLogic(savedInstanceState);
        qrcode_sn = getIntent().getStringExtra("qrcode_sn");
        ivQrcode.setImageBitmap(QRCodeUtil.createQRCodeBitmap(qrcode_sn, 300, 300));

        //每2秒请求一次接口
        pollingUtil = new PollingUtil(new Handler(getMainLooper()));

        runnable = new Runnable() {
            @Override
            public void run() {
                if (num < 900 && !isSuccess) {
                    num++;
                    requestScanResult();
                }
            }
        };
        pollingUtil.startPolling(runnable, 2000, true);
    }

    private void requestScanResult() {
        Map<String, String> map = new HashMap<>();
        //"0000000038FF7306304B503345430651"
        map.put("chipcode", preferences.getString("sn",""));
        String passWord = SignatureUtil.getPassword();
        map.put("password", passWord);
        Network.getQrCode().getQrCode(OPEN, map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(observer);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        pollingUtil.endPolling(runnable);
    }
}
